﻿using System;
using System.Collections.Generic;

namespace ProjectDeployment.Loggers
{
    class Logger
    {
        private static object mLock = new object();
        private static Logger mLogger = null;
        private List<ILogger> mObservers;

        public static Logger instance
        {
            get
            {
                if(mLogger == null)
                {
                    lock (mLock)
                    {
                        if(mLogger == null)
                        {
                            mLogger = new Logger();
                        }
                    }
                }
                return mLogger;
            }
        }

        private Logger()
        {
            mObservers = new List<ILogger>();
        }
        
        public void RegisterObserver(ILogger observer)
        {
            if (!mObservers.Contains(observer))
            {
                mObservers.Add(observer);
            }
        }

        public void AddLogMessage(string message)
        {
            var formattedMessage = message;

            foreach (var observer in mObservers)
            {
                observer.ProcessLogMessage(formattedMessage);
            }
        }
    }
}
